package com.ruix.app.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.FloatMath;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ruix.app.R;

/**
 * Created by hadexs on 16/4/16.
 */
public class TabItemView extends RelativeLayout {

    private Context mContext;
    private String name, amountNum;
    private int icon, icon_action;
    private OnClickListener listener;
    private boolean haveNum;
    private boolean selected;

    public void setClickListener(OnClickListener listener) {
        this.listener = listener;
    }

    public TabItemView(Context context) {
        super(context);
    }

    public TabItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initAttributes(attrs);
        initView();
    }

    private void initAttributes(AttributeSet attributeSet) {
        if (null != attributeSet) {

            TypedArray array = getContext().obtainStyledAttributes(attributeSet,
                    R.styleable.TabItemView);
            CharSequence t1 = array.getText(0);

            icon = array.getResourceId(R.styleable.TabItemView_tab_icon, 0);
            icon_action = array.getResourceId(R.styleable.TabItemView_tab_icon_action, 0);
            array.recycle();
            if (t1 != null) {
                name = t1.toString();
            }

        }

    }


    public TabItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    ImageView iconView,redPointView;
    TextView text, count;

    private void initView() {
        LinearLayout.inflate(mContext, R.layout.tab_item_main, this);
        iconView = (ImageView) findViewById(R.id.iv_tab_icon_view);
        redPointView = (ImageView) findViewById(R.id.iv_tab_red_point);
        text = (TextView) findViewById(R.id.tv_tab_name);

        if (icon != 0) {
            iconView.setImageResource(icon);
        }
        if (!TextUtils.isEmpty(name)) {
            text.setText(name);
        }
//        if (!TextUtils.isEmpty(amountNum)) {
//            count = (TextView) findViewById(R.id.amount_num_tv);
//            count.setVisibility(View.VISIBLE);
//            count.setText(amountNum);
//        }

    }


    float downX;
    float downY;

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
//                iconView.setFocusable(true);
//                text.setFocusable(true);

                downX = event.getX();
                downY = event.getY();
                iconView.setImageResource(icon_action);
                text.setTextColor(mContext.getResources().getColor(R.color.green));
                return true;
//                break;
            case MotionEvent.ACTION_MOVE:
                float moveX = event.getX();
                float moveY = event.getY();
                float d = distance(downX, moveX, downY, moveY);

                if (!selected && d > 30) {
                    iconView.setImageResource(icon);
                    text.setTextColor(mContext.getResources().getColor(R.color.gray));
                }
                break;
            case MotionEvent.ACTION_UP:
                float upX = event.getX();
                float upY = event.getY();
                float upDistance = distance(downX, upX, downY, upY);

                if (upDistance < 50) {
                    if (listener != null) {
                        listener.onClick(this);
                    }
                }else if (selected){
                    iconView.setImageResource(icon_action);
                    text.setTextColor(mContext.getResources().getColor(R.color.green));
                }else {
                    iconView.setImageResource(icon);
                    text.setTextColor(mContext.getResources().getColor(R.color.gray));
                }
                break;
        }

        return super.onTouchEvent(event);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    private float distance(float x0, float x1, float y0, float y1) {
        float x = x0 - x1;
        float y = y0 - y1;
        return (float) Math.sqrt(x * x + y * y);
    }


    public void setSelectedStatus(boolean status) {
        selected = status;
        if (status) {
            iconView.setImageResource(icon_action);
            text.setTextColor(mContext.getResources().getColor(R.color.green));
        } else {
            iconView.setImageResource(icon);
            text.setTextColor(mContext.getResources().getColor(R.color.gray));
        }
    }

    public interface OnClickListener {
        void onClick(View v);
    }


    public void setAmount(int shit) {
        String tabNum;
        if (shit >= 0 && count != null) {
            if (shit > 99) {
                tabNum = "99+";
            } else {
                tabNum = String.valueOf(shit);
            }
            count.setText(tabNum);
            count.setVisibility(View.VISIBLE);
        }
    }

    public void setNewMessageState(boolean has){
        if (has){
            redPointView.setVisibility(VISIBLE);
        }else {
            redPointView.setVisibility(GONE);
        }
    }

}